<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<title></title>
</head>

<body>
	<script>
		document.addEventListener('plusready', initPlusready)
		async function initPlusready() {
			// return createWebView('../website/index.html')
			plus.nativeUI.showWaiting('加载配置中...');
			await initWebsite()
			await initDianshizhibo()
			plus.nativeUI.closeWaiting();
			plus.storage.removeItem("pandaBoxStorageCountdown");
			createWebView(plus.io.convertLocalFileSystemURL('_doc/pages/website/index.html'))
		}
		function closeListener() {
			// createWebView('../website/index.html')
			createWebView(plus.io.convertLocalFileSystemURL('_doc/pages/website/index.html'))
		}
		function createWebView(url) {
			var websiteWebview = plus.webview.create(url, 'websiteWebview', {
				videoFullscreen: 'landscape',
				kernel: 'WKWebview',
				hardwareAccelerated: true,
			});
			websiteWebview.removeEventListener('close', closeListener)
			websiteWebview.addEventListener('close', closeListener)

			// websiteWebview.appendJsFile('../website/index.js');
			websiteWebview.appendJsFile(plus.io.convertLocalFileSystemURL('_doc/pages/website/index.js'));
			websiteWebview.show()

			// 返回键退出
			var first = null;
			plus.key.addEventListener('backbutton', function () {
				websiteWebview.canBack(function (e) {
					if (e.canBack) {
						websiteWebview.back();
					} else {
						//处理逻辑：1秒内，连续两次按返回键，则退出应用；
						//首次按键，提示‘再按一次退出应用’
						if (!first) {
							first = new Date().getTime();
							plus.nativeUI.toast('再按一次退出应用');
							setTimeout(function () {
								first = null;
							}, 1000);
						} else {
							if (new Date().getTime() - first < 1500) {
								plus.runtime.quit();
							}
						}
					}
				})
			});
		}

		function initWebsite() {
			return new Promise(async (resolve, reject) => {
				const remotes = [
					{
						"url": "https://gitee.com/shudazhang/panda-box-public/raw/master/pages/website/index.html",
						"local": '_doc/pages/website/index.html',
						"timeout": 2000,
					},
					{
						"url": "https://gitee.com/shudazhang/panda-box-public/raw/master/pages/website/index.js",
						"local": '_doc/pages/website/index.js',
						"timeout": 2000,
					}
				]
				for (const remote of remotes) {
					try {
						await removeFile(remote.local)
						await downloader(remote.local, remote.url, remote.timeout)
					} catch (error) {
						
					}
				}
				resolve()
			})
		}

		function initDianshizhibo() {
			return new Promise(async (resolve, reject) => {
				const remotes = [
					{
						"url": "https://gitee.com/shudazhang/panda-box-public/raw/master/pages/dianshizhibo/index.html",
						"local": '_doc/pages/dianshizhibo/index.html',
						"timeout": 2000,
					},
					{
						"url": "https://gitee.com/shudazhang/panda-box-public/raw/master/pages/dianshizhibo/index.js",
						"local": '_doc/pages/dianshizhibo/index.js',
						"timeout": 2000,
					},
					{
						"url": "https://gitee.com/shudazhang/panda-box-public/raw/master/pages/dianshizhibo/hls-1.6.7.min.js",
						"local": '_doc/pages/dianshizhibo/hls-1.6.7.min.js',
						"timeout": 2000,
					}
				]
				for (const remote of remotes) {
					try {
						await removeFile(remote.local)
						await downloader(remote.local, remote.url, remote.timeout)
					} catch (error) {
						
					}
				}
				resolve()
			})
		}


		function removeFile(localPath) {
			return new Promise((resolve, reject) => {
				var filePath = plus.io.convertLocalFileSystemURL(localPath);
				plus.io.resolveLocalFileSystemURL(filePath, function (entry) {
					entry.remove(function () {
						resolve()
					}, function (error) {
						reject(error)
					});
				}, function (error) {
					resolve()
				});
			})
		}

		function downloader(localPath, remotePath, timeout) {
			return new Promise((resolve, reject) => {
				var dtask = plus.downloader.createDownload(remotePath, {
					method: 'GET',
					filename: localPath,
					timeout: timeout / 1000,
					retry: 0
				}, function (d, status) {
					if (status === 200) {
						resolve()
					} else {
						reject()
					}
				});
				dtask.start();
			})
		}
	</script>
</body>

</html>